/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.sec;

import han.bifang.business.sec.access.Authenticator;
import han.bifang.business.sec.access.AuthenticatorFactory;
import han.bifang.business.sec.access.LoginContext;
import han.bifang.business.sec.rbac.RbacProvider;
import han.bifang.business.sec.rbac.RbacProviderFactory;
import han.bifang.common.entity.SoaResource;
import han.bifang.common.entity.SoaRole;
import han.bifang.common.entity.SoaUser;
import han.bifang.common.entity.SoaUsergroup;
import han.bifang.common.exception.BifangException;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Kent
 */
public class SecurityManager {
	private static RbacProvider provider = RbacProviderFactory
			.getRbacProvider();

	private static Authenticator auth = AuthenticatorFactory.getAuthenticator();

	public static String createUser(SoaUser user) throws BifangException {
		return provider.getUserProvider().createUser(user);
	}

	public static void modifyUser(SoaUser user) throws BifangException {
		provider.getUserProvider().modifyUser(user);
	}

	public static void removeUser(String userId) throws BifangException {
		provider.getUserProvider().removeUser(userId);
	}

	public static SoaUser getUserByLoginName(String loginName)
			throws BifangException {
		return provider.getUserProvider().getUserByLoginName(loginName);
	}

	public static SoaUser getUserById(String id) throws BifangException {
		return provider.getUserProvider().getUserById(id);
	}

	public static List findUsersByPage(String loginName, String userName,
			int page, int pageSize) throws BifangException {
		return provider.getUserProvider().findUsersByPage(loginName, userName,
				page, pageSize);
	}

	public static List<SoaUsergroup> getGroupsOfUser(String userId)
			throws BifangException {
		return provider.getUserProvider().getGroupsOfUser(userId);
	}

	public static List<SoaUsergroup> getGroupsNotOfUserByPage(String userId,
			String groupName, int page, int pageSize) throws BifangException {
		return provider.getUserProvider().getGroupsNotOfUserByPage(userId,
				groupName, page, pageSize);
	}

	public static ArrayList getUserDetail(String id) throws BifangException {
		ArrayList ret = new ArrayList();
		SoaUser u = getUserById(id);
		if (u != null) {
			ret.add(u);
			List<SoaUsergroup> groups = getGroupsOfUser(id);
			ret.add(groups);
			return ret;
		}
		return null;
	}

	public static void addGroupsOfUser(String[] groupIds, String userId)
			throws BifangException {
		provider.getUserProvider().addGroupsOfUser(groupIds, userId);
	}

	public static void removeGroupsOfUser(String[] groupIds, String userId)
			throws BifangException {
		provider.getUserProvider().removeGroupsOfUser(groupIds, userId);
	}

	// ------------group-----------------
	public static String createGroup(SoaUsergroup group) throws BifangException {
		return provider.getGroupProvider().createGroup(group);
	}

	public static void modifyGroup(SoaUsergroup group) throws BifangException {
		provider.getGroupProvider().modifyGroup(group);
	}

	public static void removeGroup(String groupId) throws BifangException {
		provider.getGroupProvider().removeGroup(groupId);
	}

	public static SoaUsergroup getGroupByName(String groupName)
			throws BifangException {
		return provider.getGroupProvider().getGroupByName(groupName);
	}

	public static SoaUsergroup getGroupById(String id) throws BifangException {
		return provider.getGroupProvider().getGroupById(id);
	}

	public static List findGroupsByPage(String groupName, int page, int pageSize)
			throws BifangException {
		return provider.getGroupProvider().findGroupsByPage(groupName, page,
				pageSize);
	}

	public static List getUsersOfGroupByPage(String groupId, int page,
			int pageSize) throws BifangException {
		return provider.getGroupProvider().getUsersOfGroupByPage(groupId, page,
				pageSize);

	}

	public static List getUsersNotOfGroupByPage(String groupId,
			String userName, String loginName, int page, int pageSize)
			throws BifangException {
		return provider.getGroupProvider().getUsersNotOfGroupByPage(groupId,
				userName, loginName, page, pageSize);
	}

	public static void addUsersInGroup(String[] userIds, String groupId)
			throws BifangException {
		provider.getGroupProvider().addUsersInGroup(userIds, groupId);
	}

	public static void removeUsersFromGroup(String[] userIds, String groupId)
			throws BifangException {
		provider.getGroupProvider().removeUsersFromGroup(userIds, groupId);
	}

	public static List<SoaRole> getRolesOfGroup(String groupId)
			throws BifangException {
		return provider.getGroupProvider().getRolesOfGroup(groupId);
	}

	public static List getRolesNotOfGroupByPage(String groupId,
			String roleName, int page, int pageSize) throws BifangException {
		return provider.getGroupProvider().getRolesNotOfGroupByPage(groupId,
				roleName, page, pageSize);
	}

	public static ArrayList getGroupDetail(String id) throws BifangException {
		ArrayList ret = new ArrayList();
		SoaUsergroup u = getGroupById(id);
		if (u != null) {
			ret.add(u);
			List<SoaRole> roles = getRolesOfGroup(id);
			ret.add(roles);
			return ret;
		}
		return null;
	}

	public static void addRolesOfGroup(String[] roleIds, String groupId)
			throws BifangException {
		provider.getGroupProvider().addRolesOfGroup(roleIds, groupId);
	}

	public static void removeRolesOfGroup(String[] roleIds, String groupId)
			throws BifangException {
		provider.getGroupProvider().removeRolesOfGroup(roleIds, groupId);
	}

	// -----------role------------------

	public static String createRole(SoaRole role) throws BifangException {
		return provider.getRoleProvider().createRole(role);
	}

	public static void modifyRole(SoaRole role) throws BifangException {
		provider.getRoleProvider().modifyRole(role);
	}

	public static void removeRole(String roleId) throws BifangException {
		provider.getRoleProvider().removeRole(roleId);
	}

	public static SoaRole getRoleByName(String roleName) throws BifangException {
		return provider.getRoleProvider().getRoleByName(roleName);
	}

	public static SoaRole getRoleById(String id) throws BifangException {
		return provider.getRoleProvider().getRoleById(id);
	}

	public static List findRolesByPage(String roleName, int page, int pageSize)
			throws BifangException {
		return provider.getRoleProvider().findRolesByPage(roleName, page,
				pageSize);
	}

	public static void addGroupsToRole(String[] groupdIds, String roleId)
			throws BifangException {
		provider.getRoleProvider().addGroupsToRole(groupdIds, roleId);
	}

	public static void removeGroupsFromRole(String groupId[], String roleId)
			throws BifangException {
		provider.getRoleProvider().removeGroupsFromRole(groupId, roleId);
	}

	public static List<SoaUsergroup> getGroupsOfRole(String roleId)
			throws BifangException {
		return provider.getRoleProvider().getGroupsOfRole(roleId);
	}

	public static List getGroupsNotOfRoleByPage(String roleId,
			String groupName, int page, int pageSize) throws BifangException {
		return provider.getRoleProvider().getGroupsNotOfRoleByPage(roleId,
				groupName, page, pageSize);
	}

	public static List getResourcesOfRoleByPage(String roleId, int page,
			int pageSize) throws BifangException {
		return provider.getRoleProvider().getResourcesOfRoleByPage(roleId,
				page, pageSize);
	}

	public static List getResourcesNotOfRoleByPage(String roleId,
			String resourceType, String property1, String property2,
			String property3, int page, int pageSize) throws BifangException {
		return provider.getRoleProvider().getResourcesNotOfRoleByPage(roleId,
				resourceType, property1, property2, property3, page, pageSize);
	}

	public static ArrayList getRoleDetail(String id) throws BifangException {
		ArrayList ret = new ArrayList();
		SoaRole u = getRoleById(id);
		if (u != null) {
			ret.add(u);
			List<SoaUsergroup> groups = getGroupsOfRole(id);
			ret.add(groups);
			return ret;
		}
		return null;
	}

	public static void addResourcesToRole(String[] resourceIds, String roleId)
			throws BifangException {
		provider.getRoleProvider().addResourcesToRole(resourceIds, roleId);
	}

	public static void removeResourcesFromRole(String[] resourceIds,
			String roleId) throws BifangException {
		provider.getRoleProvider().removeResourcesFromRole(resourceIds, roleId);
	}

	// resource
	public static String createResource(SoaResource r) throws BifangException {
		return provider.getResourceProvider().createResource(r);
	}

	public static void modifyResource(SoaResource r) throws BifangException {
		provider.getResourceProvider().modifyResource(r);
	}

	public static void removeResourceById(String resourceId)
			throws BifangException {
		provider.getResourceProvider().removeResourceById(resourceId);
	}

	public static void removeResourceByEntity(String entityId, String entityType)
			throws BifangException {
		provider.getResourceProvider().removeResourceByEntity(entityId,
				entityType);

	}

	public static void addRolesToResource(String[] roleIds, String resourceId)
			throws BifangException {
		provider.getResourceProvider().addRolesToResource(roleIds, resourceId);
	}

	public static void removeRolesFromResource(String[] roleIds,
			String resourceId) throws BifangException {
		provider.getResourceProvider().removeRolesFromResource(roleIds,
				resourceId);
	}

	public static List<SoaRole> getRolesOfResource(String resourceId)
			throws BifangException {
		return provider.getResourceProvider().getRolesOfResource(resourceId);
	}

	public static List getRolesNotOfResourceByPage(String resourceId,
			String roleName, int page, int pageSize) throws BifangException {
		return provider.getResourceProvider().getRolesNotOfResourceByPage(
				resourceId, roleName, page, pageSize);
	}

	public static SoaResource getResourceById(String id) throws BifangException {
		return provider.getResourceProvider().getResourceById(id);
	}

	public static SoaResource getResourceByEntity(String entityId,
			String entityType) throws BifangException {
		return provider.getResourceProvider().getResourceByEntity(entityId,
				entityType);
	}

	// auth

	public static boolean checkUser(LoginContext ctx)
			throws BifangException {
		return auth.checkUser( ctx);
	}

	public static boolean checkPermission(String loginName, String serviceCode)
			throws BifangException {
		return auth.checkPermission(loginName, serviceCode);
	}

}
